<?php
/**
 * 单品基类
 * productcontroller.php
 * Create on 2017-03-07 09:57:31
 * Create by linlin
 */
namespace haibao\cms\business\control;

use haibao\frame\data\query\Pager;
use \haibao\cms\model\filter\newproduct\Product as ProductFilter;
use \haibao\cms\model\filter\Brand as BrandFilter;
use \haibao\cms\model\filter\newproduct\ProductBrandGroup as ProductBrandGroupFilter;
use \haibao\cms\model\filter\newproduct\ProductCategoryDefine as CategoryFilter;
use \haibao\cms\model\filter\newproduct\ProductCategoryRelation as CategoryRelationFilter;
use \haibao\cms\model\filter\User as UserFilter;
use \haibao\cms\model\filter\newproduct\Goods as GoodsModelFilter;
use \haibao\frame\http\Request;
use \haibao\frame\data\query\Condition;
use haibao\cms\web\view\app\article\Preview;

class ProductController extends \haibao\cms\business\control\pc\channel\ChannelController{
	
    const PAGE_SIZE = 20;
    const WAP_PAGE_SIZE = 10;
    const PC_PAGE_SIZE = 20;
	const NEWSUPDATE = 1;
	const PRO_FIVE = 5;//文章4：3
	const PRO_FOUR = 4;//文章1：1
	const PRO_TWO = 2;//图集4：3
	const PRO_ONE = 1;//图集1：1
	const PRO_ZERO = 0;//默认
    private static $_instance;
    public $baseUrl = '';
	public function __construct($updateTime='',$controlId='',$isPublish=''){
		parent::__construct($updateTime,$controlId,$isPublish);
	}

	public static function getProductInstance(){
	    if(!(self::$_instance instanceof self)){
	        self::$_instance = new self;
	    }
	    return self::$_instance;
	}
	
	public function displayTemplateHtml(){
	    $controlData = $this->getParameter(self::$CONTROL_DATA);
	    $content = $this->getParameter(self::$CONTROL_CONTENT);
	    $path = $this->generateHtmlPath(__CLASS__);
	    $isNewHtml = $this->generateHtml($path,$content);
	    $content = $this->getHtml($path);
	    $content = $this->replaceCDNLink($content);
	    return $content;
	}
	
	public static function getProductSearchUrl($type = 0,$station = 0,$date = 0,$letter = 0,$isPc = true){
	    
		return array(
				'WAP'=>\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_WAP).'/tv/ku/list_'.$type.'_'.$station.'_'.$date.'_'.$letter.'_0_0/',
				'PC' =>\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_TV_HOST).'ku/list_'.$type.'_'.$station.'_'.$date.'_'.$letter.'_0_0/',
		);
	    
	}
	
	
	
	//检索页  封装查询
	/**
	 * @param Array $condition 查询条件
	 * @return Object $productList 单品列表
	 * $isWap来源是否为WAP,true为WAP，pc为false
	 */
	public function getProductList($condition,$isGetAll = true,$isWap = false){
	    // 根据$confition 封装query filter
	    
	    $dataList = $this->getProductListByCondition($condition,$isWap);
	    return $dataList;
	}
	

	
	public function getProductListByCondition($condition,$isWap){
	    $productFilter = new ProductFilter();
	    $productFilter->select(array(
	        ProductFilter::CONDITION_FIELD_NAME_ID,
	        ProductFilter::CONDITION_FIELD_NAME_PRODUCT_NAME,
	        ProductFilter::CONDITION_FIELD_NAME_CREATETIME,
	        ProductFilter::CONDITION_FIELD_NAME_PRODUCT_SERIES,
	        ProductFilter::CONDITION_FIELD_NAME_SEASON,
	        ProductFilter::CONDITION_FIELD_NAME_SEASON_YEAR,
	        ProductFilter::CONDITION_FIELD_NAME_CREATE_BY,
	        ProductFilter::CONDITION_FIELD_NAME_STATUS,
	        ProductFilter::CONDITION_FIELD_NAME_CREATE_BY,
	    ));
	    	
	    $productBrandFilter = new ProductBrandGroupFilter();
	    $productBrandFilter->select(array(
	        ProductBrandGroupFilter::CONDITION_FIELD_NAME_BRAND_ID,
	        ProductBrandGroupFilter::CONDITION_FIELD_NAME_PRODUCT_ID,
	    ));
	    $productFilter->leftJoin($productBrandFilter,null,ProductFilter::CONDITION_FIELD_NAME_ID,ProductBrandGroupFilter::CONDITION_FIELD_NAME_PRODUCT_ID,ProductFilter::CONDITION_FIELD_NAME_BRAND_IDS);
	    	
	    $goodsModelFilter = new GoodsModelFilter();
	    $goodsModelFilter->select(array(
	        GoodsModelFilter::CONDITION_FIELD_NAME_ID,
	    ));
	    
	    $goodsModelFilter->where(GoodsModelFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\Product::STATUS_ENABLE);
	    $productFilter->leftJoin($goodsModelFilter,null,ProductFilter::CONDITION_FIELD_NAME_ID,GoodsModelFilter::CONDITION_FIELD_NAME_PRODUCT_ID,ProductFilter::CONDITION_FIELD_NAME_PRODUCT_MODEL);
	    	
	    $categoryRelationFilter = new CategoryRelationFilter();
	    $categoryRelationFilter->select(array(
	        CategoryRelationFilter::CONDITION_FIELD_NAME_ID,
	        CategoryRelationFilter::CONDITION_FIELD_NAME_CATEGORY_ID,
	    ));
	    $productFilter->leftJoin($categoryRelationFilter,null,ProductFilter::CONDITION_FIELD_NAME_ID,CategoryRelationFilter::CONDITION_FIELD_NAME_PRODUCT_ID,ProductFilter::CONDITION_FIELD_NAME_CATEGORY_IDS);
	    	
	    $productFilter->order(ProductFilter::CONDITION_FIELD_NAME_CREATETIME,false);
	    
	    $productFilter->setPager(new Pager(self::PAGE_SIZE,$condition['page']));
	    $pager = $goodsModelFilter->getPager();
	    $productList = \haibao\cms\business\newproduct\Product::getProduct($productFilter);
	    
	    
	    echo '<pre>';
	    print_r($productList);
	    exit; 
	    
	    
	    
	    
	    
	    /* $goodsFilter = new \haibao\cms\model\filter\newproduct\Goods();
	    $goodsFilter->select(array(
	        $goodsFilter::CONDITION_FIELD_NAME_ID,
	        $goodsFilter::CONDITION_FIELD_NAME_PRODUCT_ID,
	        $goodsFilter::CONDITION_FIELD_NAME_PRODUCT_NAME,
	    ));
	    
	    $goodsFilter->where($goodsFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, 1);
	    $goodsFilter->order($goodsFilter::CONDITION_FIELD_NAME_CREATETIME,false);
	    
	    $productFilter = new \haibao\cms\model\filter\newproduct\Product();
	    $productFilter->select(array(
	        $productFilter::CONDITION_FIELD_NAME_ID,
	        $productFilter::CONDITION_FIELD_NAME_PRODUCT_SERIES,
	        $productFilter::CONDITION_FIELD_NAME_SEASON_YEAR
	    ));
	    
	    $productCategoryFilter = new \haibao\cms\model\filter\newproduct\ProductCategoryRelation();
	    $productCategoryFilter->select(array(
	        $productCategoryFilter::CONDITION_FIELD_NAME_PRODUCT_ID,
	        $productCategoryFilter::CONDITION_FIELD_NAME_CATEGORY_ID
	    ));
	    
	    $prodcutCategoryNameFilter = new \haibao\cms\model\filter\newproduct\ProductCategoryDefine();
	    $prodcutCategoryNameFilter->select(array(
	        $prodcutCategoryNameFilter::CONDITION_FIELD_NAME_ID,
	        $prodcutCategoryNameFilter::CONDITION_FIELD_NAME_CATEGORY_NAME
	    ));
	    
	    
	    $goodsFilter->leftJoin($productFilter,null,$goodsFilter::CONDITION_FIELD_NAME_PRODUCT_ID,$productFilter::CONDITION_FIELD_NAME_ID,$goodsFilter::CONDITION_FIELD_NAME_PRODUCT_GOODS_INFO,false);
	    $goodsFilter->leftJoin($productCategoryFilter,null,$goodsFilter::CONDITION_FIELD_NAME_PRODUCT_ID,$productCategoryFilter::CONDITION_FIELD_NAME_PRODUCT_ID,$goodsFilter::CONDITION_FIELD_NAME_PRODUCT_CATEGORY_INFO,false);
	    
	    $productList = \haibao\cms\business\newproduct\Product::getProduct($productFilter);

	    $goodsFilter->setPager(new Pager(self::PAGE_SIZE,$condition['page']));
	    $pager = $goodsFilter->getPager();
	    //类型
	    if($condition['category']){
	        $productCategoryFilter = new \haibao\cms\model\filter\newproduct\ProductCategoryRelation();
	        $productCategoryFilter->select(array(
    	         $productCategoryFilter::CONDITION_FIELD_NAME_PRODUCT_ID,
	             $productCategoryFilter::CONDITION_FIELD_NAME_CATEGORY_ID
    	    ));
	        $goodsFilter->leftJoin($productCategoryFilter,null,$goodsFilter::CONDITION_FIELD_NAME_ID,$productCategoryFilter::CONDITION_FIELD_NAME_PRODUCT_ID,$goodsFilter::CONDITION_FIELD_NAME_PRODUCT_CATEGORY_INFO,false);
	        $productCategoryFilter->where($productCategoryFilter::CONDITION_FIELD_NAME_CATEGORY_ID, Condition::CONDITION_EQUAL, (int)$condition['category']);
	    } */
	
	    //品牌ID
	    /* if($condition['brandId']){
	        $americanRadioFilter = new AmericanRadioFilter();
	        $americanRadioFilter->select(array(
	            AmericanRadioFilter::CONDITION_FIELD_NAME_ID,
	            AmericanRadioFilter::CONDITION_FIELD_NAME_AMERICANID,
	            AmericanRadioFilter::CONDITION_FIELD_NAME_RADIOID
	        ));
	        $americanFilter->leftjoin($americanRadioFilter, null, $americanFilter::CONDITION_FIELD_NAME_ID, $americanRadioFilter::CONDITION_FIELD_NAME_AMERICANID, $americanFilter::CONDITION_FIELD_NAME_AMERICANRADIO);
	        $americanRadioFilter->where(AmericanRadioFilter::CONDITION_FIELD_NAME_RADIOID, Condition::CONDITION_EQUAL, (int)$condition['station']);
	    } */
	   
	    
	    return array($productList,$pager);
	    //return $designerList;
	}
	
}
